/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.code.pic;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import z.h.w.jar.kit.method.Foto;
import z.h.w.jar.kit.method.IO;
import z.h.w.jar.kit.method.Roll;
import z.h.w.jar.kit.method.Self;
import z.h.w.jar.kit.clazz.Watch;

/**
 *
 * @author EnzoZhong
 */
public class String2Image implements Code {

      private BufferedImage image;
      private Graphics2D g;
      private Boolean borader = false;

      /**
       *
       * @param color
       * @param fontSize
       * @param font
       * @param string
       */
      public String2Image ( Color color , Integer fontSize , Font font , String string ) {
            image = new BufferedImage ( fontSize * string.length () * 2 , fontSize * 2 , BufferedImage.TYPE_4BYTE_ABGR );
            g = image.createGraphics ();

            g.setColor ( color );
            font = font.deriveFont ( Float.valueOf ( fontSize ) );
            g.setFont ( font );
            g.setRenderingHint ( RenderingHints.KEY_ANTIALIASING , RenderingHints.VALUE_ANTIALIAS_ON );
            g.drawString ( string , 1 , fontSize + 1 );
      }

      private static Map<Map<Object , Object> , String2Image> cacheMap;

      public static String2Image born ( Color color , Integer fontSize , Font font , String string ) {
            cacheMap = cacheMap == null ? new HashMap<Map<Object , Object> , String2Image> ( 8 ) : cacheMap;
            Map<Object , Object> key = new HashMap<> ( 6 );
            key.put ( color , color );
            key.put ( fontSize , fontSize );
            key.put ( font , font );
            key.put ( string , string );

            if ( cacheMap.containsKey ( key ) ) {
                  return cacheMap.get ( key );
            } else {
                  String2Image string2Image = new String2Image ( color , fontSize , font , string );
                  cacheMap.put ( key , string2Image );
                  return string2Image;
            }
      }

      public static String2Image born ( Integer fontSize , Font font , String string ) {
            return born ( Color.BLACK , fontSize , font , string );
      }

      public void setBorader ( Boolean borader ) {
            this.borader = borader;
      }

      private BufferedImage reImage;

      /**
       *
       * @return
       */
      @Override
      public BufferedImage getImage () {
            if ( reImage == null ) {
                  Foto.Border minMax = Foto.findBorder ( image );
                  Point min = minMax.min;
                  Point max = minMax.max;
                  if ( borader ) {
                        Graphics2D gg = ( Graphics2D ) image.getGraphics ();
                        gg.setColor ( Color.BLACK );
                        gg.drawRect ( min.x - 1 , min.y - 1 , max.x - min.x + 2 , max.y - min.y + 2 );
                        minMax = Foto.findBorder ( image );
                        min = minMax.min;
                        max = minMax.max;
                  }
                  reImage = image.getSubimage ( min.x , min.y , max.x - min.x + 1 , max.y - min.y + 1 );
                  return reImage;
            } else {
                  return reImage;
            }
      }

      /**
       *
       * @return
       */
      @Override
      public String getString () {
            throw new UnsupportedOperationException ( "Not supported yet." );
      }

      /**
       *
       * @param args
       */
      public static void main ( String[] args ) {
            //Color color, Integer fontSize, Font font, String string 
            Watch watch = new Watch ();
            for ( int x = 0 ; x < 100 ; x ++ ) {
                  String2Image water = String2Image.born ( 100 , Roll.roll ( Self.getChineseFonts () ) , "呆呆 我 非常爱你！！" );
//                  String2Image water = String2Image.born ( Color.BLACK , 100 , new Font ( "宋体" , Font.BOLD , 20 ) , "呆呆 我 非常爱你！！" );
                  IO.bufferedImage2PngC ( water.getImage () );
            }
            watch.stop ();

      }

}
